﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Data.Filtering;
using DevExpress.XtraEditors;
using System.Threading.Tasks;
using DevExpress.Xpo;

namespace RemoteImaging.LicensePlate
{
    public partial class FormSuspectCarManager : XtraForm
    {
        private readonly IMessageBoxService _messageBoxService;
        private MyBindingList<ReportedCarInfo> _carInfos;
        private List<ReportedCarInfo> _removedCarInfos = new List<ReportedCarInfo>();
        private readonly IVisualStateManager _visualStateManager;
        
        static FormSuspectCarManager()
        {
            AutoMapper.Mapper.CreateMap<SuspectCarInfoDto, ReportedCarInfo>()
                .ForMember(r => r.Oid, opt => opt.Ignore())
                .ForMember(r => r.UpdateTime, opt => opt.Ignore());
            AutoMapper.Mapper.AssertConfigurationIsValid();
        }


        public List<ReportedCarInfo> RemovedInfos
        {
            get { return _removedCarInfos; }
        }

        public FormSuspectCarManager()
        {
            InitializeComponent();

            UpcatePageIndicator();
        }

        public FormSuspectCarManager(IMessageBoxService messageBoxService,
                                     IVisualStateManager visualStateManager)
            : this()
        {
            if (visualStateManager == null)
                throw new ArgumentNullException("visualStateManager", "visualStateManager is null.");
            _visualStateManager = visualStateManager;
            if (messageBoxService == null) throw new ArgumentNullException("messageBoxService");
            _messageBoxService = messageBoxService;
        }


        private void suspectCarGridView_InitNewRow(object sender,
            DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            //var row = (ReportedCarInfo)suspectCarGridView.GetRow(e.RowHandle);
            //row.CreationTime = DateTime.Now;
        }

        private void importAdd_Click(object sender, EventArgs e)
        {
        }



        private void okButton_Click(object sender, EventArgs e)
        {
            //suspectCars.
        }

        private void importDeactive_Click(object sender, EventArgs e)
        {
        }

        private void advBandedGridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {

        }

        private void next_Click(object sender, EventArgs e)
        {
            xpPageSelector1.CurrentPage++;

            UpcatePageIndicator();
        }

        private void UpcatePageIndicator()
        {
            currentPage.Text = string.Format("{0}/{1}页", xpPageSelector1.CurrentPage+1, xpPageSelector1.PageCount);
            prev.Enabled = xpPageSelector1.CurrentPage > 0;
            next.Enabled = xpPageSelector1.CurrentPage < xpPageSelector1.PageCount - 1;
        }

        private void prev_Click(object sender, EventArgs e)
        {
            if (xpPageSelector1.CurrentPage > 0)
            {
                xpPageSelector1.CurrentPage--;
            }

            UpcatePageIndicator();
            
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            refreshGrid();
        }

        private void lpnToLookup_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                refreshGrid();
            }
        }
        private void refreshGrid()
        {
            Cursor = Cursors.WaitCursor;

            try
            {
                //string cstr = string.Format("LicenseNumber like '%{0}%'", lpnToLookup.Text);
                //var c = CriteriaOperator.Parse(cstr);
                //suspectCars.Criteria = c;
                //suspectCars.Reload();

                //reloadPage();
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void reloadPage()
        {
            var ps = new XPPageSelector(suspectCars);
            ps.PageSize = xpPageSelector1.PageSize;
            gridControl1.DataSource = ps;
            xpPageSelector1 = ps;

            UpcatePageIndicator();
        }
    }
}